package se.webbzon.boltzmann.game.object.dialogue.factory;

import java.util.Collection;

import se.webbzon.boltzmann.game.object.dialogue.layout.DialogueLayout;
import se.webbzon.boltzmann.game.object.dialogue.word.AbstractDialogueWord;
import se.webbzon.boltzmann.game.object.dialogue.word.ImageDialogueWord;
import se.webbzon.boltzmann.item.AbstractItem;
import se.webbzon.boltzmann.item.ItemGroup;

public class ItemDialogueWordFactory extends AbstractDialogueWordFactory {
	
	// The name (or id) of item
	private final String itemName;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new image dialogue word factory with a given texture
	 * set name. **/
	public ItemDialogueWordFactory(String itemName) {
		this.itemName = itemName;
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public AbstractDialogueWord createWord(DialogueLayout layout) {
		AbstractItem item;
		try {
			int id = Integer.parseInt(itemName);
			item = ItemGroup.instance().getItem(id);
		} catch (NumberFormatException e) {
			Collection<AbstractItem> items = ItemGroup.instance().getItems(itemName);
			if (items.isEmpty())
				item = null;
			else
				item = items.iterator().next();
		}
		
		if (item == null)
			return new ImageDialogueWord(layout,"item_none");
		else
			return new ImageDialogueWord(layout,item.getTextureSet());
	}

}
